package cloudoer.blog.controller;

import cloudoer.blog.base.BaseController;
import cloudoer.blog.entity.Article;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("article")
public class ArticleController extends BaseController {
    private static Logger logger = Logger.getLogger(ArticleController.class);

    @RequestMapping("index")
    public String index (){
        return "admin/article/index";
    }

    @RequestMapping("/addUI")
    public String addUI(){
        return "admin/article/add";
    }
    @RequestMapping("/updateUI")
    public String updateUI(){
        return "admin/article/update";
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public Map<String, Object> getAll (){
        List<Article> list = articleService.getAll();
        return getMap(list);
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Article getById (String id){
        Article a = articleService.getById(id);
        return a;
    }

    @RequestMapping("/getByType")
    @ResponseBody
    public Map<String, Object> getByType (String typeId){
        List<Article> list = articleService.getByType(typeId);
        return getMap(list);
    }

    @RequestMapping("/getOrderDate")
    @ResponseBody
    public Map<String, Object> getOrderDate (){
        List<Article> list = articleService.getByOrder("postdate");
        return getMap(list);
    }

    @RequestMapping("/getOrderBrowse")
    @ResponseBody
    public Map<String, Object> getOrderBrowse (){
        List<Article> list = articleService.getByOrder("browsecount");
        return getMap(list);
    }

    @RequestMapping("/getOrderEndorse")
    @ResponseBody
    public Map<String, Object> getOrderEndorse (){
        List<Article> list = articleService.getByOrder("endorsecount");
        return getMap(list);
    }

    @RequestMapping("/getOrderRank")
    @ResponseBody
    public Map<String, Object> getOrderRank (){
        List<Article> list = articleService.getByOrder("ranking");
        return getMap(list);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(Article article) throws IOException {
        logger.info(article);
        articleService.update(article);
        return success("添加成功");
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update (Article article){
        articleService.update(article);
        return success("修改成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete (String id,HttpServletRequest request){
        articleService.delete(id,
                new File(request.getSession().getServletContext().getRealPath("/") +
                        File.separator + "file" + File.separator + id));
        return success("删除成功");
    }

    @RequestMapping("/endorse")
    @ResponseBody
    public String endorse(String id){
        System.out.println(id);
        return String.valueOf(articleService.endorse(id));
    }

    @RequestMapping("/image")
    @ResponseBody
    public Map<String, Object> image(HttpServletRequest request,MultipartFile image,String id) throws IOException {
        if (image == null || StringUtils.isBlank(id) || "null".equals(id)){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("error", 1);
            map.put("message", "文件为空");
            return map;
        }
        //以文章id创建一个文件夹并提前将id存入数据库，如果文章被删除，将删除这个文件夹及其所有文件
        File filePath = new File(request.getSession().getServletContext().getRealPath("/")
                + File.separator + "file" + File.separator + id);
        if (!filePath.exists()) {
            filePath.mkdirs();
            Article article = new Article();
            article.setId(id);
            article.setPostdate(new Date());
            articleService.add(article);
        }
        return saveImage(image,id,filePath);
    }
}
